package com.hwh.traffic.adapter;

import android.content.Context;
import android.database.Cursor;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.cursoradapter.widget.SimpleCursorAdapter;



import com.hwh.traffic.MapApplication;
import com.hwh.traffic.db.TrafficLab;


public class AutoCompleteAdapter extends SimpleCursorAdapter {

    private Context context;
    // 查询字段
    private String queryField;
    private TrafficLab trafficLab = null;

    public AutoCompleteAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);

        this.context = context;
        this.queryField = from[0];
        MapApplication application = (MapApplication)context.getApplicationContext();
        trafficLab = application.getTrafficLab();
    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (constraint != null) {
            //return trafficLab.findStopCuror((String) constraint);
            return trafficLab.findStopAndRoute((String) constraint);
        } else {
            return null;
        }
    }
    @Override
    public CharSequence convertToString(Cursor cursor) {
        return cursor.getString(cursor.getColumnIndex(queryField));
    }
}
